package com.auntvt.timo.jpax.springbootstarter.service.impl;

import com.auntvt.timo.jpax.core.exception.JpaxRepositoryException;
import com.auntvt.timo.jpax.springbootstarter.repository.JpaxRepository;
import com.auntvt.timo.jpax.springbootstarter.service.JpaxService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Service基类-Simple（泛型：R Repository接口  T 实体类、ID 主键类型）
 *
 * @author 小懒虫 <auntvt@163.com>
 * @date 2021/5/25
 */
public class SimpleServiceImpl<R, T, ID> extends JpaxServiceImpl<T, ID> implements JpaxService<T, ID> {
    /**
     * 对应实体类的持久层Repository
     */
    @Autowired
    protected R repository;

    /**
     * 获取对应实体类的持久层Repository接口
     *
     * @return Repository接口
     */
    @Override
    @SuppressWarnings("unchecked")
    public JpaxRepository<T, ID> getRepository() {
        if (!(repository instanceof JpaxRepository)) {
            throw new JpaxRepositoryException("[{0}] 没有继承BaseRepository接口", repository.getClass().getName());
        }
        return (JpaxRepository<T, ID>) repository;
    }
}
